<?php
class control_user extends core_action{
	
	function __construct(){
		if($this->isAdminLogin() === false){
			$this->redirict(Qtpl::createUrl('index', 'login',array(),'admin'));
		}
	}
	
	function index(){
		$model = new core_model_user();
		$page = intval($_REQUEST['p']) ? intval($_REQUEST['p']) : 1;
		$model->setPage($page);
		$model->setLimit(core_lib_constant::PAGE_NUM);
		$model->setCount(TRUE);
		$rs = $model->select("","","","order by uid desc");
		$params['user'] = $rs->items;
		$params['pageSize'] = $rs->pageSize;
		$params['page'] = $page;
		$params['user_cur'] = 'current';
		$params['token'] = md5($_COOKIE['adminKey']);
		$this->render('simpla/user_list.php', $params);
	}
	
	function show(){
		$uid = intval($_REQUEST['uid']);
		$user = new core_service_user($uid);
		$rs = $user->get();
		if(!$rs){
			$this->showMsg("用户不存在",2);
		}
		if($_POST){
			$post = Qutil::filter($_POST);
			if($post['token'] != md5($_COOKIE['adminKey'])){
				$this->showMsg("非法请求",2);
			}
			if($post['pwd']){
				if($user->changePassword($post['pwd']) === false){
					$this->showMsg("修改密码出错".$user->getError(),2);
				}
			}
			if($post['lock'] == 1){
				$user->locks(intval($post['day']));
			}else{
				if($rs['lock'] == 1){
					$user->unLocks();
				}
			}
			if($user->edit($post) === false){
				$this->showMsg("编辑出错".$user->getError(),2);
			}
			$model = new core_model_adminlog();
			$model->addLog(4, "编辑了用户 {$rs['username']} 的资料");
			$this->showMsg("编辑成功",1,Qtpl::createUrl('user', 'index','','admin'));
		}
		$params['user'] = $rs;
		$params['user_cur'] = 'current';
		$params['token'] = md5($_COOKIE['adminKey']);
		$this->render('simpla/user.php', $params);
	}
}